package com.locus.locus.client;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.locus.locus.client.dados.LocalizacaoWrapper;
import com.locus.locus.client.dados.SiteWrapper;
import com.locus.locus.client.widgets.WidgetBox;

public class Site implements EntryPoint {
	
	private final ServidorAsync servidor = GWT.create(Servidor.class);
	private final AsyncCallback<SiteWrapper> callbackSite = new AsyncCallback<SiteWrapper>() {
		@Override
		public void onSuccess(SiteWrapper siteWrapper) {
			site = siteWrapper;
			carregarSite();
		}
		
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("falhou em buscar dados do site");
		}
	};
	
	private SiteWrapper site;

	@Override
	public void onModuleLoad() {
		final String idEstabelecimento = Window.Location.getParameter("eid");
		this.servidor.buscarSite(idEstabelecimento, this.callbackSite);
	}

	private void carregarSite() {
		//TODO criar o postador de tweets
		//RootPanel.get("postar_tweet").add()
		final HorizontalPanel painel = new HorizontalPanel();
		final VerticalPanel painelInfoMap = new VerticalPanel();
		final TextArea info = new TextArea();
		final List<LocalizacaoWrapper> localizacoes = this.site.getLocalizacoes();
		final WidgetBox boxes = new WidgetBox(this.site.getNomeSite(), this.site.getTweets());
		
		RootPanel.get("root").add(painel);

		//boxes.setWidth("70%");
		painel.add(boxes);
		painel.add(painelInfoMap);
		painelInfoMap.add(info);
		
		info.setEnabled(false);
		info.setText(this.site.getInformacoes());
		info.setCharacterWidth(35);
		info.setVisibleLines(6);
		
		for(LocalizacaoWrapper localizacao:localizacoes){
			final DockLayoutPanel dock = new DockLayoutPanel(Unit.PX);
			final LatLng latLng = LatLng.newInstance(localizacao.getLatitude(), localizacao.getLongitude());
	
		    final MapWidget map = new MapWidget(latLng, 15);
		    //map.add
		    
		    map.setSize("450px","400px");
		    map.addControl(new LargeMapControl());	
		    map.addOverlay(new Marker(latLng));
	
		    // Add an info window to highlight a point of interest
		    map.getInfoWindow().open(map.getCenter(),
		        new InfoWindowContent(this.site.getNomeSite()));
	
		    dock.addNorth(map, 500);
		   
		    painelInfoMap.add(dock);
	    }
	}
}
